﻿#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WaveEngine.Common.Math;
using WaveEngine.Framework;
using WaveEngine.Framework.Graphics;
using WaveEngine.Framework.Services;
using WaveEngine.Framework.UI; 
#endregion

namespace WaveEngine.Extensions.Behaviors
{
    public class StretchBehavior : Behavior
    {
        [RequiredComponent]
        private Transform2D transform = null;

        public Thickness Margin;

        private float cachedRatioX;
        private float cachedRatioY;

        public StretchBehavior()
        {
            this.Margin = new Thickness(0);
        }

        protected override void Update(TimeSpan gameTime)
        {
            if (this.cachedRatioX != WaveServices.ViewportManager.RatioX
             || this.cachedRatioY != WaveServices.ViewportManager.RatioY)
            {
                var realWidth = WaveServices.ViewportManager.RightEdge - WaveServices.ViewportManager.LeftEdge;
                var realHeight = WaveServices.ViewportManager.BottomEdge - WaveServices.ViewportManager.TopEdge;

                this.transform.X = WaveServices.ViewportManager.LeftEdge + this.Margin.Left;
                this.transform.Y = WaveServices.ViewportManager.TopEdge + this.Margin.Top;

                this.transform.Rectangle = new RectangleF(
                    0,
                    0,
                    realWidth - this.Margin.Left - this.Margin.Right,
                    realHeight - this.Margin.Top - this.Margin.Bottom);

                this.cachedRatioX = WaveServices.ViewportManager.RatioX;
                this.cachedRatioY = WaveServices.ViewportManager.RatioY;
            }
        }
    }
}
